﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using TestMySqlConnetPoll;
using System.Threading;
using System.Collections;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.Utils.Drawing;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Repository;
using DevExpress.Utils;
using DevExpress.XtraGrid.Columns;
using DevExpress.Data;
using DevExpress.Skins;

namespace View.History
{
   
    public partial class HistroyQXtraForm : DevExpress.XtraEditors.XtraForm
    {
        private List<AlarmType> alarmList = new List<AlarmType>();
        private List<string> equList = new List<string>();
        private List<string> equTypeList = new List<string>();

        private AlarmTable alarmTable = new AlarmTable();

        private bool isAllBaojin;
        private bool isAllYiJi;
        private bool isAllErJi;
        private bool isAllSanJi;
        private bool isAllYuJin;

        private List<byte> yiji;
        private List<byte> erji;
        private List<byte> sanji;
        private List<byte> yujin;

        /// <summary>
        /// 查询变量定义
        /// </summary>
        private DateTime sDateTime = DateTime.MinValue;
        private DateTime eDateTime = DateTime.MinValue;
        private List<string> alarmType = new List<string>();
        private List<int> alarmLevel = new List<int>();
        private string devType = "";
        private string devName = "";


        private BindingList<HistroyAlarmObj> source = new BindingList<HistroyAlarmObj>();

        /// <summary>
        /// 构造函数
        /// </summary>
        public HistroyQXtraForm()
        {
            //DevExpress.Utils.AppearanceObject.DefaultFont = new System.Drawing.Font("宋体", 9); //解决输出PDF乱码

            InitializeComponent();

            this.gridView1.CustomDrawCell += gridView1_CustomDrawCell;
            this.gridView1.ShowingEditor += gridView1_ShowingEditor;
            this.DevDataGrid.DataSourceChanged += DevDataGrid_DataSourceChanged;


            treeList_Alarm_Type.Nodes[0].Expanded = true;
            treeList_Alarm_Type.CheckAll();

            LoadDeviceList();//初始化设备类型及设备

            comboBox_Type.EditValue = comboBox_Type.Properties.Items[0];
            comboBox_Type.SelectedIndexChanged += new EventHandler(comboBoxEdit1_SelectedIndexChanged);

            dateEdit_S.EditValue = DateTime.Now;
            dateEdit_E.EditValue = DateTime.MinValue;
            //selection = new GridCheckMarksSelection(gridView1);

            foreach (string equ in comboBoxEdit_Dev.Properties.Items)
            {
                equTypeList.Add(equ);
            }

            foreach (string equ in comboBox_Type.Properties.Items)
            {
                equList.Add(equ);
            }

            this.treeList_Alarm_Type.Nodes.Clear();
            this.treeList_Alarm_Type.Nodes.Add(new object[] {"全部"});
            foreach (KeyValuePair<int, List<string>> keyValue in c_Device.Alarm_ConfigList)
            {
                if (keyValue.Key == 1)
                    this.treeList_Alarm_Type.Nodes[0].Nodes.Add(new object[] {"一级报警"});
                else if (keyValue.Key == 2)
                    this.treeList_Alarm_Type.Nodes[0].Nodes.Add(new object[] {"二级报警"});
                else if (keyValue.Key == 3)
                    this.treeList_Alarm_Type.Nodes[0].Nodes.Add(new object[] {"三级报警"});
                else if (keyValue.Key == 4)
                    this.treeList_Alarm_Type.Nodes[0].Nodes.Add(new object[] {"预警"});

                foreach (string strLevel in keyValue.Value)
                {
                    this.treeList_Alarm_Type.Nodes[0].Nodes[keyValue.Key - 1].Nodes.Add(new object[] { strLevel });
                }

            }

            this.treeList_Alarm_Type.Nodes[0].CheckAll();

            SetAlarmInfo();
            col_level.Group();
            col_level.FieldNameSortGroup = "AlarmLevel_int";
            this.gridView1.ExpandGroupLevel(1);
        }

        void gridView1_ShowingEditor(object sender, CancelEventArgs e)
        {
            string val = Convert.ToString(this.gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["statue"]));
            if (val != null && val.Contains("已处理"))
            {
                e.Cancel = true;
            }          
        }

        void gridView1_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
        {
            string val = gridView1.GetRowCellValue(e.RowHandle, "statue") as string;
            if (val != null && val.Contains("已处理"))
            {
                e.Appearance.BackColor = Color.DarkGray;
                //cell.RowInfo.Appearance.BackColor = CommonColors.GetCriticalColor(DevExpress.LookAndFeel.UserLookAndFeel.Default);
                //e.Handled = true;
            }                  
        }

        //private GridCheckMarksSelection selection;
        void DevDataGrid_DataSourceChanged(object sender, EventArgs e)
        {
            
            //selection.CheckMarkColumn.VisibleIndex = 0;
            //selection.CheckMarkColumn.Width = 30;
            //selection.CheckMarkColumn.OptionsColumn.AllowEdit = false;
        }


        void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {            
            //comboBoxEdit_Dev.Properties.Items.Clear();
            DevName_comboBoxEdit.Properties.Items.Clear();
            DevName_comboBoxEdit.EditValue = "";
            devType = type_comboBoxEdit.EditValue as string;
            foreach (objMsgDevStruct dev in c_Device.DeviceMsgGather[(string)type_comboBoxEdit.EditValue].DevList)
            {
                //comboBoxEdit_Dev.Properties.Items.Add(dev.DevName);
                DevName_comboBoxEdit.Properties.Items.Add(dev.DevName);
            }
            if (DevName_comboBoxEdit.Properties.Items.Count > 0)
            {
                DevName_comboBoxEdit.EditValue = DevName_comboBoxEdit.Properties.Items[0];
            }
            //comboBoxEdit_Dev.EditValue = comboBoxEdit_Dev.Properties.Items[0];
        }

        /// <summary>
        /// 加载设备列表
        /// </summary>
        private void LoadDeviceList()
        {
            foreach (string strKey in c_Device.DeviceMsgGather.Keys)
            {
                //comboBox_Type.Properties.Items.Add(strKey);
                type_comboBoxEdit.Properties.Items.Add(strKey);
            }
        }

        private void GetTreeListNode(TreeListNode obj)
        {
            if (obj.CheckState == CheckState.Checked)
            {
                alarmType.Add(obj.GetValue(0) as string);
                return;
            }
            else if (obj.CheckState == CheckState.Indeterminate)
            {
                for (int i = 0; i < obj.Nodes.Count; i++)
                {
                    if (obj.Nodes[i].CheckState == CheckState.Checked)
                    {
                        alarmType.Add(obj.Nodes[i].GetValue(0) as string);
                    }
                    else if (obj.Nodes[i].CheckState == CheckState.Indeterminate)
                    {
                        for (int j = 0; j < obj.Nodes[i].Nodes.Count; j++)
                        {
                            GetTreeListNode(obj.Nodes[i].Nodes[j]);
                        }
                    }                    
                }
            }
            else return;
        }

        private void SetAlarmInfo()
        {
            alarmType.Clear();
            alarmLevel.Clear();
            source.Clear();
            GetTreeListNode(treeList_Alarm_Type.Nodes[0]);

            if (sDateTime == DateTime.MinValue)
            {
                MessageBox.Show("请选择查询开始时间");
                return;
            }
            if ((devType != "") && (devName == ""))
            {
                MessageBox.Show("请选择需要查询的设备，不支持查询一种设备类型的报警!");
                return;
            }
            if (alarmType.Contains("全部"))
            {
                alarmLevel.Clear();
                alarmType.Clear();
            }
            if (alarmType.Contains("一级报警")) alarmLevel.Add(1);
            if (alarmType.Contains("二级报警")) alarmLevel.Add(2);
            if (alarmType.Contains("三级报警")) alarmLevel.Add(3);
            if (alarmType.Contains("预警")) alarmLevel.Add(4);

            alarmType.Remove("一级报警");
            alarmType.Remove("二级报警");
            alarmType.Remove("三级报警");
            alarmType.Remove("预警");

            alarmTable.Read_ValueList(sDateTime, eDateTime, alarmType, alarmLevel, devType, devName);

            while (!alarmTable.Ready_AlarmValueList)
            {
                if (alarmTable.ErrorFlag != 0)
                {
                    return;
                }

                Thread.Sleep(20);
            }
            foreach (byte key in alarmTable.ValueList.Keys)
            {
                foreach (ObjAlaram alarm in alarmTable.ValueList[key])
                {
                    source.Add(MySql.ConvertAlarmObjToDisplay(alarm));
                }
            }
            FilterDisplayData();            
        }

        private void treeList1_AfterCheckNode(object sender, NodeEventArgs e)
        {
            if (e.Node.CheckState == CheckState.Checked)
            {
                alarmType.Add(e.Node.GetValue(0) as string);
            }
            else
            {
                if (alarmType.Contains(e.Node.GetValue(0) as string))
                {
                    alarmType.Remove(e.Node.GetValue(0) as string);
                }                
            }
            SetCheckedChildNodes(e.Node, e.Node.CheckState);
            SetCheckedParentNodes(e.Node, e.Node.CheckState);
        }

        private void treeList1_BeforeCheckNode(object sender, CheckNodeEventArgs e)
        {
            e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);

        }

        private void SetCheckedChildNodes(TreeListNode node, CheckState check)
        {
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                node.Nodes[i].CheckState = check;

                if (check == CheckState.Checked)
                {
                    alarmType.Add(node.Nodes[i].GetValue(0) as string);
                }
                else
                {
                    if(alarmType.Contains(node.Nodes[i].GetValue(0) as string))
                    {
                        alarmType.Remove(node.Nodes[i].GetValue(0) as string);
                    }                    
                }
                SetCheckedChildNodes(node.Nodes[i], check);
            }
        }

        private void SetCheckedParentNodes(TreeListNode node, CheckState check)
        {
            if (node.ParentNode != null)
            {
                bool b = false;
                CheckState state;
                for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
                {
                    state = (CheckState)node.ParentNode.Nodes[i].CheckState;
                    if (!check.Equals(state))
                    {
                        b = !b;
                        break;
                    }
                }
                node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;

                if (check == CheckState.Checked)
                {
                    alarmType.Add(node.ParentNode.GetValue(0) as string);
                }
                else
                {
                    if (alarmType.Contains(node.ParentNode.GetValue(0) as string))
                    {
                        alarmType.Remove(node.ParentNode.GetValue(0) as string);
                    }
                }

                SetCheckedParentNodes(node.ParentNode, check);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSearch_Click(object sender, EventArgs e)
        {
            alarmList.Clear();
            //(this.DevDataGrid.MainView as DevExpress.XtraGrid.Views.Grid.GridView).DeleteRow
            SetAlarmInfo();
        }

        private void BtnPrint_Click(object sender, EventArgs e)
        {
            this.DevDataGrid.ShowRibbonPrintPreview();
        }

        private void DevDataGrid_Click(object sender, EventArgs e)
        {
           
        }

        /// <summary>
        /// 报警确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnConfirm_Click(object sender, EventArgs e)
        {
            if (childRows != null && childRows.Length > 0)
            {
                FrmUserConFrimAlarm frm = new FrmUserConFrimAlarm();
                frm.list = _selection;
                frm.ShowDialog();

                SetAlarmInfo();
            }
        }

        private void sDateTimeChange(object sender, EventArgs e)
        {
            sDateTime = dateEdit_S.DateTime;
        }

        private void eDateTimeChanged(object sender, EventArgs e)
        {
            eDateTime = dateEdit_E.DateTime;
        }

        private void devNameChanged(object sender, EventArgs e)
        {
            devName = DevName_comboBoxEdit.EditValue as string;
        }

        private void checkEdit_dateEnd_CheckedChanged(object sender, EventArgs e)
        {
            if (!checkEdit_dateEnd.Checked)
            {
                this.dateEdit_E.Visible = false;
                eDateTime = DateTime.MinValue;
            }
            else
            {
                eDateTime = DateTime.Now;
                dateEdit_E.EditValue = DateTime.Now;
                this.dateEdit_E.Visible = true;
            }
        }

        private void  FilterDisplayData()
        {
            BindingList<HistroyAlarmObj> li_1 = new BindingList<HistroyAlarmObj>();
            if (check_cl.CheckState == CheckState.Checked)
            {
                for (int i = 0; i < source.Count; i++)
                {
                    HistroyAlarmObj obj = source[i];
                    if (obj.statue != null)
                    {
                        if (!obj.statue.Contains("处理")) li_1.Add(obj);
                    }
                    else li_1.Add(obj);
                }
            }
            else li_1 = source;

            BindingList<HistroyAlarmObj> li_2 = new BindingList<HistroyAlarmObj>();
            if (check_not_cl.CheckState == CheckState.Checked)
            {
                for (int i = 0; i < li_1.Count; i++)
                {
                    HistroyAlarmObj obj = li_1[i];
                    if (obj.statue != null)
                    {
                        if (obj.statue.Contains("处理")) li_2.Add(obj);
                    }
                }
            }
            else li_2 = li_1;

            BindingList<HistroyAlarmObj> li_3 = new BindingList<HistroyAlarmObj>();
            if (check_hf.CheckState == CheckState.Checked)
            {
                for (int i = 0; i < li_2.Count; i++)
                {
                    HistroyAlarmObj obj = li_2[i];
                    if (obj.statue != null)
                    {
                        if (!obj.statue.Contains("恢复")) li_3.Add(obj);
                    }
                    else li_3.Add(obj);
                }
            }
            else li_3 = li_2;

            BindingList<HistroyAlarmObj> li_4 = new BindingList<HistroyAlarmObj>();
            if (check_not_hf.CheckState == CheckState.Checked)
            {
                for (int i = 0; i < li_3.Count; i++)
                {
                    HistroyAlarmObj obj = li_3[i];
                    if (obj.statue != null)
                    {
                        if (obj.statue.Contains("恢复")) li_4.Add(obj);
                    }
                }
            }
            else li_4 = li_3;
            this.DevDataGrid.DataSource = li_4;
            this.gridView1.BestFitColumns();
        }

        private void check_not_hf_CheckedChanged(object sender, EventArgs e)
        {
            if(check_not_hf.CheckState == CheckState.Checked)
               check_hf.CheckState = CheckState.Unchecked;
            FilterDisplayData();            
        }

        private void check_not_cl_CheckedChanged(object sender, EventArgs e)
        {
            if (check_not_cl.CheckState == CheckState.Checked)
                check_cl.CheckState = CheckState.Unchecked;
            FilterDisplayData();
        }

        private void check_hf_CheckedChanged(object sender, EventArgs e)
        {
            if (check_hf.CheckState == CheckState.Checked)
                check_not_hf.CheckState = CheckState.Unchecked;
            FilterDisplayData();
        }

        private void check_cl_CheckedChanged(object sender, EventArgs e)
        {
            if (check_cl.CheckState == CheckState.Checked)
                check_not_cl.CheckState = CheckState.Unchecked;
            FilterDisplayData();
        }
        public ArrayList _selection = new ArrayList();

        private int[] childRows = null;

        private void gridView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this._selection.Clear();
            childRows = this.gridView1.GetSelectedRows();

            for (int i = 0; i < childRows.Length; i++)
            {
                object obj = this.gridView1.GetRow(childRows[i]);

                if (obj != null)
                    this._selection.Add(obj);
            }
        }

        private void DevDataGrid_MouseClick(object sender, MouseEventArgs e)
        {
            if(e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (this.gridView1.FocusedRowHandle >= 0)
                    rightMenu.Show(this.PointToScreen(e.Location));
            }
        }

        private void replayMenu_Click(object sender, EventArgs e)
        {

        }

        private void barCheckLevel_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if(barCheckLevel.Checked)
            {
                col_level.Group();
                barCheckCSM.Checked = false;
                barCheckDevName.Checked = false;
                barCheckType.Checked = false;
                this.gridView1.ExpandGroupLevel(1);
            }
            else
            {
                col_level.UnGroup();
            }
        }

        private void barCheckType_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (barCheckType.Checked)
            {
                col_atype.Group();
                barCheckCSM.Checked = false;
                barCheckDevName.Checked = false;
                barCheckLevel.Checked = false;
                this.gridView1.ExpandGroupLevel(1);
            }
            else
            {
                col_atype.UnGroup();
            }
        }

        private void barCheckCSM_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (barCheckCSM.Checked)
            {
                col_devType.Group();
                barCheckType.Checked = false;
                barCheckDevName.Checked = false;
                barCheckLevel.Checked = false;
                this.gridView1.ExpandGroupLevel(1);
            }
            else
            {
                col_devType.UnGroup();
            }
        }

        private void barCheckDevName_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (barCheckDevName.Checked)
            {
                col_devName.Group();
                barCheckCSM.Checked = false;
                barCheckLevel.Checked = false;
                barCheckType.Checked = false;
                this.gridView1.ExpandGroupLevel(1);
            }
            else col_devName.UnGroup();
        }
    }

    public class AlarmType
    {
        public string name { get; set; }
        public string type { get; set; }
        public string detail { get; set; }
        public string alarmLevel { get; set; }
        public int alarmCount { get; set; }
    }

    public class GridCheckMarksSelection
    {

        public event EventHandler SelectionChanged;

        protected GridView _view;
        public ArrayList _selection;
        private GridColumn _column;
        private RepositoryItemCheckEdit _edit;

        public GridView View
        {
            get { return _view; }
            set
            {
                if (_view == value)
                    return;
                if (_view != null)
                    Detach();
                _view = value;
                Attach();
            }
        }

        public GridColumn CheckMarkColumn { get { return _column; } }
        public int SelectedCount { get { return _selection.Count; } }

        public GridCheckMarksSelection()
        {
            _selection = new ArrayList();
        }

        public GridCheckMarksSelection(GridView view)
            : this()
        {
            this.View = view;
        }

        protected virtual void Attach()
        {
            if (View == null)
                return;
            _selection.Clear();
            _view = View;
            _edit = View.GridControl.RepositoryItems.Add("CheckEdit")
                as RepositoryItemCheckEdit;
            _edit.EditValueChanged += edit_EditValueChanged;
            _column = View.Columns.Insert(0);
            _column.OptionsColumn.AllowSort = DefaultBoolean.False;
            _column.VisibleIndex = int.MinValue;
            _column.FieldName = "CheckMarkSelection";
            _column.Caption = "Mark";
            _column.OptionsColumn.ShowCaption = false;
            _column.UnboundType = UnboundColumnType.Boolean;
            _column.ColumnEdit = _edit;


            View.CustomDrawColumnHeader += View_CustomDrawColumnHeader;
            View.CustomDrawGroupRow += View_CustomDrawGroupRow;
            View.CustomUnboundColumnData += view_CustomUnboundColumnData;

            View.MouseUp += view_MouseUp;

            

        }

        protected virtual void Detach()
        {
            if (_view == null)
                return;
            if (_column != null)
                _column.Dispose();
            if (_edit != null)
            {
                _view.GridControl.RepositoryItems.Remove(_edit);
                _edit.Dispose();
            }
            _view.CustomDrawColumnHeader -= View_CustomDrawColumnHeader;
            _view.CustomDrawGroupRow -= View_CustomDrawGroupRow;
            _view.CustomUnboundColumnData -= view_CustomUnboundColumnData;
            _view.MouseDown -= view_MouseUp;
            _view = null;
        }

        protected virtual void OnSelectionChanged(EventArgs e)
        {
            if (SelectionChanged != null)
                SelectionChanged(this, e);
        }

        protected void DrawCheckBox(Graphics g, Rectangle r, bool Checked)
        {
            var info = _edit.CreateViewInfo() as CheckEditViewInfo;
            var painter = _edit.CreatePainter() as CheckEditPainter;
            ControlGraphicsInfoArgs args;
            info.EditValue = Checked;
            info.Bounds = r;
            info.CalcViewInfo(g);
            args = new ControlGraphicsInfoArgs(info, new GraphicsCache(g), r);
            painter.Draw(args);
            args.Cache.Dispose();
        }

        private void view_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Clicks == 1 && e.Button == MouseButtons.Left)
            {
                GridHitInfo info;
                var pt = _view.GridControl.PointToClient(System.Windows.Forms.Control.MousePosition);
                info = _view.CalcHitInfo(pt);
                if (info.InColumn && info.InRow && _view.IsDataRow(info.RowHandle))
                    UpdateSelection();
                if (info.InColumn && info.Column == _column)
                {
                    if (SelectedCount == _view.DataRowCount)
                        ClearSelection();
                    else
                        SelectAll();
                }
                if (info.InRow && _view.IsGroupRow(info.RowHandle)
                    && info.HitTest != GridHitTest.RowGroupButton)
                {
                    bool selected = IsGroupRowSelected(info.RowHandle);
                    SelectGroup(info.RowHandle, !selected);
                }
            }
        }

        private void View_CustomDrawColumnHeader
            (object sender, ColumnHeaderCustomDrawEventArgs e)
        {
            if (e.Column != _column)
                return;
            e.Info.InnerElements.Clear();
            e.Painter.DrawObject(e.Info);
            //DrawCheckBox(e.Graphics, e.Bounds, SelectedCount == _view.DataRowCount);_selection
            DrawCheckBox(e.Graphics, e.Bounds, SelectedCount == _selection.Count);
            e.Handled = true;
        }

        private void View_CustomDrawGroupRow
            (object sender, RowObjectCustomDrawEventArgs e)
        {
            var info = e.Info as GridGroupRowInfo;
            info.GroupText = "         " + info.GroupText.TrimStart();
            e.Info.Paint.FillRectangle
               (e.Graphics, e.Appearance.GetBackBrush(e.Cache), e.Bounds);
            e.Painter.DrawObject(e.Info);
            var r = info.ButtonBounds;
            r.Offset(r.Width * 2, 0);
            DrawCheckBox(e.Graphics, r, IsGroupRowSelected(e.RowHandle));
            e.Handled = true;
        }

        private void view_CustomUnboundColumnData
            (object sender, CustomColumnDataEventArgs e)
        {
            if (e.Column != CheckMarkColumn)
                return;
            if (e.IsGetData)
                e.Value = IsRowSelected(View.GetRowHandle(e.ListSourceRowIndex));
            else
                SelectRow(View.GetRowHandle(e.ListSourceRowIndex), (bool)e.Value);
        }

        private void edit_EditValueChanged(object sender, EventArgs e)
        {
            _view.PostEditor();
        }

        private void SelectRow(int rowHandle, bool select, bool invalidate)
        {
            if (IsRowSelected(rowHandle) == select)
                return;
            object row = _view.GetRow(rowHandle);
            if (select)
                _selection.Add(row);
            else
                _selection.Remove(row);
            if (invalidate)
                Invalidate();
            OnSelectionChanged(EventArgs.Empty);
        }

        public object GetSelectedRow(int index)
        {
            return _selection[index];
        }

        public int GetSelectedIndex(object row)
        {
            return _selection.IndexOf(row);
        }
        public void ClearSelection()
        {
            _selection.Clear();
            View.ClearSelection();
            Invalidate();
            OnSelectionChanged(EventArgs.Empty);
        }

        private void Invalidate()
        {
            _view.CloseEditor();
            _view.BeginUpdate();
            _view.EndUpdate();
        }

        public void SelectAll()
        {
            _selection.Clear();
            var dataSource = _view.DataSource as ICollection;
            //if (dataSource != null && dataSource.Count == _view.DataRowCount)
            //    _selection.AddRange(dataSource);  // fast
            //else
            //    for (int i = 0; i < _view.DataRowCount; i++)  // slow
            //        _selection.Add(_view.GetRow(i));
            if (dataSource != null)
                for (int i = 0; i < _view.DataRowCount; i++)  // slow
                {
                    if ((_view.GetRow(i) as HistroyAlarmObj).statue != "已处理")
                    _selection.Add(_view.GetRow(i));
                }
                   

            Invalidate();
            OnSelectionChanged(EventArgs.Empty);
        }

        public void SelectGroup(int rowHandle, bool select)
        {
            if (IsGroupRowSelected(rowHandle) && select) return;
            for (int i = 0; i < _view.GetChildRowCount(rowHandle); i++)
            {
                int childRowHandle = _view.GetChildRowHandle(rowHandle, i);
                if (_view.IsGroupRow(childRowHandle))
                    SelectGroup(childRowHandle, select);
                else
                    SelectRow(childRowHandle, select, false);
            }
            Invalidate();
        }

        public void SelectRow(int rowHandle, bool select)
        {
            SelectRow(rowHandle, select, true);
        }

        public bool IsGroupRowSelected(int rowHandle)
        {
            for (int i = 0; i < _view.GetChildRowCount(rowHandle); i++)
            {
                int row = _view.GetChildRowHandle(rowHandle, i);
                if (_view.IsGroupRow(row))
                    if (!IsGroupRowSelected(row))
                        return false;
                    else
                        if (!IsRowSelected(row))
                            return false;
            }
            return true;
        }

        public bool IsRowSelected(int rowHandle)
        {
            if (_view.IsGroupRow(rowHandle))
                return IsGroupRowSelected(rowHandle);
            object row = _view.GetRow(rowHandle);
            return GetSelectedIndex(row) != -1;
        }

        public void UpdateSelection()
        {
            //_selection.Clear();
            Array.ForEach(View.GetSelectedRows(), item => SelectRow(item, true));
        }
    }
}